// ==UserScript==
// @name         BatchURLExtractor
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Extract URLs from broker-house-list with Apple-style UI
// @author       You
// @match        https://my.58.com/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // 添加 Apple 风格的 CSS
    GM_addStyle(`
        .url-extractor-btn {
            position: fixed;
            top: 20px;
            left: 20px;
            padding: 8px 16px;
            background: rgba(0, 122, 255, 0.9);
            color: white;
            border: none;
            border-radius: 8px;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.2s ease;
            z-index: 9999;
        }

        .url-extractor-btn:hover {
            background: rgba(0, 122, 255, 1);
            transform: scale(1.02);
        }

        .url-extractor-modal {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(255, 255, 255, 0.95);
            padding: 24px;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
            z-index: 10000;
            width: 75%;
            height: 75vh;
            max-width: none;
            backdrop-filter: blur(10px);
        }

        .url-extractor-close {
            position: absolute;
            top: 24px;
            right: 24px;
            width: 32px;
            height: 32px;
            border: none;
            background: none;
            cursor: pointer;
            padding: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            opacity: 0.6;
            transition: opacity 0.2s ease;
            z-index: 10001;
        }

        .url-extractor-textarea {
            width: 100%;
            height: calc(100% - 140px);
            margin: 16px 0 16px 0;
            padding: 12px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            font-family: -apple-system, BlinkMacSystemFont, monospace;
            font-size: 14px;
            resize: none;
            background: rgba(255, 255, 255, 0.8);
        }

        .url-extractor-copy-btn {
            background: rgba(0, 122, 255, 0.9);
            color: white;
            border: none;
            border-radius: 8px;
            padding: 12px 24px;
            cursor: pointer;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.2s ease;
            display: block;
            margin: 0 0 0 auto;
            box-shadow: 0 2px 6px rgba(0, 122, 255, 0.2);
        }
    `);

    // 创建提取按钮
    const extractButton = document.createElement('button');
    extractButton.className = 'url-extractor-btn';
    extractButton.textContent = '提取链接';
    document.body.appendChild(extractButton);

    // 创建模态框
    const modal = document.createElement('div');
    modal.className = 'url-extractor-modal';
    // 修改模态框的 HTML
    modal.innerHTML = `
        <button class="url-extractor-close"></button>
        <h2 style="margin: 0 0 16px 0; font-family: -apple-system, BlinkMacSystemFont;">提取的链接</h2>
        <textarea class="url-extractor-textarea" readonly></textarea>
        <button class="url-extractor-copy-btn">复制到剪贴板</button>
    `;

    document.body.appendChild(modal);

    // 获取元素
    const textarea = modal.querySelector('.url-extractor-textarea');
    const copyButton = modal.querySelector('.url-extractor-copy-btn');

    // 点击提取按钮事件
    extractButton.addEventListener('click', () => {
        const links = [];
        const houseLinks = document.querySelectorAll('ul.broker-house-list a.house-pic');
        
        houseLinks.forEach(link => {
            links.push({
                "link": link.href
            });
        });

        textarea.value = JSON.stringify(links, null, 2);
        modal.style.display = 'block';
    });

    // 点击复制按钮事件
    copyButton.addEventListener('click', () => {
        textarea.select();
        document.execCommand('copy');
        copyButton.textContent = '已复制！';
        setTimeout(() => {
            copyButton.textContent = '复制到剪贴板';
        }, 2000);
    });

    // 点击模态框外部关闭
    document.addEventListener('click', (e) => {
        if (!modal.contains(e.target) && e.target !== extractButton) {
            modal.style.display = 'none';
        }
    });

    // 添加关闭按钮事件
    const closeButton = modal.querySelector('.url-extractor-close');
    closeButton.addEventListener('click', () => {
        modal.style.display = 'none';
    });

    // 修改点击外部关闭的逻辑
    document.addEventListener('click', (e) => {
        if (!modal.contains(e.target) && e.target !== extractButton) {
            modal.style.display = 'none';
        }
    });
})();